package com.iot08.admin.service;

import com.iot08.admin.service.page.PageVO;
import com.iot08.admin.service.page.PageableImpl;
import com.iot08.common.entity.admin.SysUser;
import com.iot08.common.entity.app.PxAdverster;
import com.iot08.common.service.base.BaseService;
import lombok.extern.slf4j.Slf4j;
import org.bson.types.ObjectId;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author cwj
 * @description
 * @date 2025/4/10 10:48
 **/
@Slf4j
@Service
public class PxAdversterService extends BaseService<PxAdverster> {

    @Resource
    private OperationService operationService;

    @Resource
    private PxAdversterConfigurationService pxAdversterConfigurationService;


    /**
     * @description 查询广告商西悉尼
     * @author cwj
     * @param [page]
     * @return org.springframework.data.domain.Page<com.iot08.common.entity.app.PxAdverster>
     * @date @time 2025/4/10 17:35
     **/
    public Page<PxAdverster> list(PageVO page) {
        PageableImpl pageable = new PageableImpl();
        page.setSort(Sort.by(Sort.Order.desc("createTime")));
        pageable.setPage(page);
        Query query = new Query();
        long count = this.count(query);
        page.setTotalResult((int) count);
        List<PxAdverster> list = this.find(query.with(pageable));
        Page<PxAdverster> result = new PageImpl<PxAdverster>(list, pageable, count);
        return result;
    }


    /**
     * @description 查询指定状态的广告商
     * @author cwj
     * @param [status]
     * @return java.util.List<com.iot08.common.entity.app.PxAdverster>
     * @date @time 2025/4/13 16:04
     **/
    public List<PxAdverster> queryAdversterByStatus(Integer status) {
        Query query = new Query();
        query.addCriteria(Criteria.where("status").is(status));
        return this.find(query);
    }

    /**
     * @description 新增广告商
     * @author cwj
     * @param [pxAdverster]
     * @return void
     * @date @time 2025/4/10 17:39
     **/
    public void add(PxAdverster pxAdverster,SysUser user, String ip) {
        if (!ObjectUtils.isEmpty(pxAdverster)  && getByShortName(pxAdverster.getShortName()) == null) {
            if(StringUtils.isEmpty(pxAdverster.getStartTime())){
                pxAdverster.setStartTime("00:00:00");
            }
            if(StringUtils.isEmpty(pxAdverster.getEndTime())){
                pxAdverster.setEndTime("23:59:59");
            }
            pxAdverster.setCreator(user.getAccount());
            pxAdverster.setUpdator(user.getAccount());
            pxAdverster.setCreateTime(new Date());
            pxAdverster.setUpdateTime(new Date());
            this.save(pxAdverster);
            operationService.add(user, ip, "广告商配置", "添加",
                    String.format("添加运营版广告配置: %s ", pxAdverster.getName()));
        }
    }


    /**
     * @description 查询广告供应商的表示是否存在
     * @author cwj
     * @param [shortName]
     * @return com.iot08.common.entity.app.PxAdverster
     * @date @time 2025/4/10 17:37
     **/
    public PxAdverster getByShortName(String shortName) {
        return this.findOne(new Query(Criteria.where("shortName").is(shortName)));
    }


    /**
     * @description 修改广告商
     * @author cwj
     * @param [pxAdverster, user, ip]
     * @return void
     * @date @time 2025/4/11 9:33
     **/
    public void update(PxAdverster pxAdverster, SysUser user, String ip) {
        PxAdverster oldPxAdverster = this.findById(new ObjectId(pxAdverster.getId()));
        //APP标识不能重复
        if(!ObjectUtils.isEmpty(oldPxAdverster) && ObjectUtils.isEmpty(getByShortNameAndId(pxAdverster.getShortName(),pxAdverster.getId()))){
            Update update = new Update();
            update.set("name", pxAdverster.getName());
            update.set("shortName", pxAdverster.getShortName());
            update.set("order", pxAdverster.getOrder());
            if(!ObjectUtils.isEmpty(pxAdverster.getStatus())) {
                update.set("status",pxAdverster.getStatus());
            }
            if(!ObjectUtils.isEmpty(pxAdverster.getWeight())) {
                update.set("weight",pxAdverster.getWeight());
            }
            if(!ObjectUtils.isEmpty(pxAdverster.getPriority())) {
                update.set("priority",pxAdverster.getPriority());
            }
            if(!StringUtils.isEmpty(pxAdverster.getStartTime())){
                update.set("startTime",pxAdverster.getStartTime());
            }
            if(!StringUtils.isEmpty(pxAdverster.getEndTime())){
                update.set("endTime",pxAdverster.getEndTime());
            }
            update.set("updator",user.getAccount());
            update.set("updateTime", new Date());
            this.updateById(new ObjectId(pxAdverster.getId()), update);
            if((StringUtils.isEmpty(oldPxAdverster.getName()) && !StringUtils.isEmpty(pxAdverster.getName())) ||
                    (!StringUtils.isEmpty(oldPxAdverster.getName()) && !oldPxAdverster.getName().equals(pxAdverster.getName()))) {
                pxAdversterConfigurationService.updateAdversterNameByAdversterId(pxAdverster.getId(),pxAdverster.getName());
            }
            operationService.add(user, ip, "广告配置", "编辑",
                    String.format("修改运营版广告配置: %s ", pxAdverster.getName()));
        }
    }

    /**
     * @description 查询是否存在App同名的广告商
     * @author cwj
     * @param [shortName, id]
     * @return com.iot08.common.entity.app.PxAdverster
     * @date @time 2025/4/11 15:51
     **/
    public PxAdverster getByShortNameAndId(String shortName,String id) {
        return this.findOne(new Query(Criteria.where("_id").ne(new ObjectId(id)).and("shortName").is(shortName)));
    }


}
